package site.lanmushan.quiz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.lanmushan.common.exception.OperateException;
import site.lanmushan.common.mapper.wrapper.DefaultWrapper;
import site.lanmushan.common.mapper.wrapper.QueryWrapper;
import site.lanmushan.common.query.service.BaseService;
import site.lanmushan.quiz.api.entity.QuizTbBank;
import site.lanmushan.quiz.api.service.IQuizTbQuestionService;
import site.lanmushan.quiz.api.service.IQuizTbBankService;
import site.lanmushan.quiz.api.vo.QuizTbBankDescVo;
import site.lanmushan.quiz.mapper.QuizTbBankMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dy
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class QuizTbBankServiceImpl extends BaseService<QuizTbBank, QuizTbBankMapper> implements IQuizTbBankService {
    @Autowired
    IQuizTbQuestionService iQuizTbQuestionService;

    @Override
    public QuizTbBankDescVo selectBankInfoByBankCode(String bankCode) {
        QueryWrapper queryWrapper = new DefaultWrapper();
        queryWrapper.eq("bank_code", bankCode);
        List<QuizTbBank> quizTbBanks = getQueryMapper().selectAllList(queryWrapper);
        if (quizTbBanks.size() == 0 || quizTbBanks.size() > 1) {
            throw new OperateException("您要测试的题库未找到");
        }
        QuizTbBankDescVo quizTbBankDescVo = BeanUtil.copyProperties(quizTbBanks.get(0), QuizTbBankDescVo.class);
        Integer integer = iQuizTbQuestionService.selectCountByBankCode(quizTbBankDescVo.getBankCode());
        quizTbBankDescVo.setQuestionSum(integer);
        return quizTbBankDescVo;

    }
}
